﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BAL
{
    public partial class ICAlInDetail : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        //Socket cliSocket = null;
        string ipAddr = "192.168.1.211";
        int port = 1024;
        private string ID;
        private string PlaceName;
        private string FBrNo;
        private string Sarte;
        public ICAlInDetail()
        {
            InitializeComponent();
        }

        public ICAlInDetail(string fno)
        {           
            InitializeComponent();
            FBrNo = fno;
        }

        public ICAlInDetail(string fno, string _ID, out int num)
        {
            InitializeComponent();
            FBrNo = fno;
            num =0;
            DataSet ds = bjs.GetICADetailIn(FBrNo);
            if(ds.Tables[0].Rows.Count>0)
            {
             num =1;
            }   
            this.ID = _ID;
        }

        private void ICAlInDetail_Load(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();
        }

        //获取数据
        private void BinData()
        {
            DataSet ds = bjs.GetICADetailIn(FBrNo);
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            dataGrid1.DataSource = dt;
            this.tb_PkgsBarCode.Text = "";
            this.tb_skname.Text = "";
            this.tb_cwname.Text = "";
            this.tb_PlaceName.Text = "";
        }
        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "ICAlDetailIn";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "流水号";
            cl1.MappingName = "FEntryNo";
            cl1.Width = 0;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "包装码";
            cl2.MappingName = "PkgsBarCode";
            cl2.Width = 80;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
            cl5.HeaderText = "助记码";
            cl5.MappingName = "HelperCode";
            cl5.Width = 60;
            dts.GridColumnStyles.Add(cl5);

            DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
            cl6.HeaderText = "调入仓库";
            cl6.MappingName = "FName";
            cl6.Width = 60;
            dts.GridColumnStyles.Add(cl6);  
            
            
            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "仓位名";
            cl4.MappingName = "PlaceName";
            cl4.Width = 120;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "SKU";
            cl3.MappingName = "ItemSku";
            cl3.Width = 200;
            dts.GridColumnStyles.Add(cl3);

            

            dataGrid1.TableStyles.Add(dts);

        }

        //查询明细
        private void tb_PkgsBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            bool lb = false;
            string skname = "";
            string NStockID = "";
            string FEntryNo = "";
            if (e.KeyCode == Keys.Enter)
            {
                string PkgsBarCode = this.tb_PkgsBarCode.Text;
                if (PkgsBarCode == "" || PkgsBarCode == null)
                {
                    MessageBox.Show("请扫描包装码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    this.tb_PkgsBarCode.Focus();
                    return;
                }
                else
                {
                    lb = bjs.GetICAIn(FBrNo, PkgsBarCode,out FEntryNo, out skname, out NStockID);
                    if (lb)
                    {
                        if (NStockID =="")
                        {
                            MessageBox.Show("此包装码在调拨单中不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                            this.tb_PkgsBarCode.Focus();
                            return;
                        }
                        this.tb_skname.Text = skname;
                        this.tb_ckid.Text = NStockID;
                        //this.tb_PlaceBarCode.Text = PlaceBarCode;
                        //this.tb_PlaceName.Text = PlaceName;
                        this.tb_cwname.Focus();
                    }
                    else
                    {
                        MessageBox.Show("扫描包装码在调拨单中不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        return;
                    }

                }

            }
        }

        private void tb_PlaceBarCode_KeyDown(object sender, KeyEventArgs e)
        {          
            string PkgsBarCode= this.tb_PkgsBarCode.Text;

            if (e.KeyCode == Keys.Enter)
            {
                string cwname = this.tb_cwname.Text;
                if (cwname == null || cwname == "")
                {
                    MessageBox.Show("请扫描仓位码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    this.tb_cwname.Focus();
                    return;
                }
                else
                {
                    string FNumber = "";//仓位代码
                    bool lb_return = bjs.GetPlaceName(cwname, out FNumber, out PlaceName);

                    if (lb_return)
                    {
                        //this.tb_cwname.Text = PlaceName;
                        //库位名称
                        tb_PlaceName.Text = tb_skname.Text + PlaceName + "-99";
                        tb_flnor.Text = "99";//暂时默认层
                        this.button1.Focus();
                    }
                    else
                    {
                        MessageBox.Show("仓位码不存在！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        tb_cwname.Focus();
                        return;
                    }   
                   

                    //states = bjs.JudgePlaceName(FBrNo,PkgsBarCode, PlaceBarCode, out PlaceName);
                    //if (states == "0")                         
                    //{
                    //    this.tb_PlaceName.Text = PlaceName;
                    //    DialogResult dr = MessageBox.Show("扫码库位与调拨入库明细单中的库位不一致，点击‘是’后提交则更新扫码库位到调拨入库明细单中", "提示信息", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    //    if (dr == DialogResult.Yes)
                    //    {
                            
                    //        this.tb_PlaceName.Text = PlaceName;
                    //        this.button1.Focus();
                    //    }
                    //}
                    //else if (states == "1")
                    //{
                    //    this.tb_PlaceName.Text = PlaceName;
                    //    this.button1.Focus();
                    //}
                    //else if (states == "3")
                    //{
                    //    MessageBox.Show("错误,请扫描仓位码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    //    this.tb_cwname.Focus();
                    //    this.tb_cwname.Text = "";
                    //    return;
                    //}                 
                }

            }
        
        }

        //选择明细
        private void dataGrid1_CurrentCellChanged(object sender, EventArgs e)
        {
            DataGrid dataGrid1 = sender as DataGrid;
            int index = ((DataGrid)sender).CurrentCell.RowNumber;
            if (((DataTable)(dataGrid1.DataSource)).Rows.Count > 0)
            {
                ((DataGrid)sender).Select(index);
            }
            //string FEntryNo = ((DataTable)(dataGrid1.DataSource)).Rows[index][0].ToString();
            //string PkgsBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index][1].ToString();
            //string PlaceBarCode = ((DataTable)(dataGrid1.DataSource)).Rows[index][2].ToString();
            //string PlaceName = ((DataTable)(dataGrid1.DataSource)).Rows[index][3].ToString();

            //this.tb_FEntryNo.Text = FEntryNo.Trim();
            //this.tb_PkgsBarCode.Text = PkgsBarCode;
            //this.tb_PlaceBarCode.Text = PlaceBarCode;
            //this.tb_PlaceName.Text = PlaceName;
        }
        //返回
        private void button2_Click(object sender, EventArgs e)
        {
            ICAlIn ica = new ICAlIn(this.ID);
            ica.Show();
            this.Hide();
        }
        //提交
        private void button1_Click(object sender, EventArgs e)
        {
            bool lb = false;
            string skname = "";
            string NStockIDnew = "";
            string FEntryNo = "";
            string NStockID = this.tb_ckid.Text.Trim();
            string cwname = this.tb_cwname.Text.Trim();
            string flnor = this.tb_flnor.Text.Trim();
            string PkgsBarCode = this.tb_PkgsBarCode.Text.Trim();

            if (PkgsBarCode == "" || PkgsBarCode == null)
            {
                MessageBox.Show("请扫描包装码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                this.tb_PkgsBarCode.Focus();
                return;
            }
           else if (cwname == null || cwname == "")
            {
                MessageBox.Show("请扫描仓位码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                this.tb_cwname.Focus();
                return;
            }
            else
            {
                   //判断包装条码是否存在
                  if (tb_ckid.Text =="")
                  {
                      bool islb = false;
                      islb = bjs.GetICAIn(FBrNo, PkgsBarCode, out FEntryNo, out skname, out NStockIDnew);
                      if (islb)
                      {
                          if (NStockIDnew == "")
                          {
                              MessageBox.Show("此包装码在调拨单中不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                              this.tb_PkgsBarCode.Focus();
                              return;
                          }
                          this.tb_skname.Text = skname;
                          this.tb_ckid.Text = NStockID;
                      }
                      else
                      {
                          MessageBox.Show("扫描包装码在调拨单中不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                          return;
                      }
                  }
                  if (tb_cwname.Text =="")
                   {
                       //判断仓位代码是否存在
                       string FNumber = "";
                       bool lb_return = bjs.GetPlaceName(cwname, out FNumber, out PlaceName);
                       if (lb_return)
                       {
                           //this.tb_cwname.Text = PlaceName;
                           //cwname = PlaceName;
                           //库位名称
                           tb_PlaceName.Text = tb_skname.Text + PlaceName + "-99";
                           tb_flnor.Text = "99";//暂时默认层
                       }
                       else
                       {
                           MessageBox.Show("仓位码不存在！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                           tb_cwname.Focus();
                           return;
                       }
                   }
                    //判断此包装条码在库存中的状态
                    bool statefalg = bjs.GetStkis(PkgsBarCode, out Sarte);
                    if (statefalg)
                    {
                        if (Sarte =="0")
                        {
                            MessageBox.Show("此包装条码信息已提交不能在进行扫描！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                            this.tb_PkgsBarCode.Focus();
                            return;
                        }
                    }
                    lb = bjs.UpdateICADetail(FBrNo,PkgsBarCode, NStockID, cwname, flnor, "4");
                    if (lb)
                    {
                        this.tb_PkgsBarCode.Focus();
                        DataGridStyle();
                        BinData();
                    }
                    else {
                        MessageBox.Show("提交失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        return;
                    }
            }
        }

    }
}